package global;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import lib.persistence.NotFoundException;

import controllers.ControllerAbstract;

/**
 * Singleton used to keep track of the single instances of all Controllers.
 * Required as a simple means of decoupling the button Actions from their view
 * @author jon
 *
 */
public class ControllerRegistry 
{
	private static ControllerRegistry instance;
	List<ControllerAbstract> controllers = new ArrayList<ControllerAbstract>();
   
	protected ControllerRegistry() {
	      // Exists only to defeat instantiation.
		
	}
	public static ControllerRegistry getInstance() {
		if(instance == null)
		{
			instance = new ControllerRegistry();
		}
	    	  
		
		return instance;
	}

	public void registerController(ControllerAbstract c)
	{
		if (!controllers.contains(c))
			controllers.add(c);
	}
	public void unregisterController(ControllerAbstract c)
	{
		if (!controllers.contains(c))
			controllers.add(c);
	}
	public ControllerAbstract getControllerByShortName(String name) throws NotFoundException
	{
		for (Iterator<ControllerAbstract> iterator = controllers.iterator(); iterator.hasNext();) {
			ControllerAbstract ca = (ControllerAbstract) iterator.next();
			
			if (ca.getShortName().equalsIgnoreCase(name))
				return ca;
		}
		throw new NotFoundException("Controller is not registered or not found");
	}
	
}
